/***************************************************************************
 创建者: 华磊
 开始时间: 2019.9.20
 copyright: (C) 华友高科
 ***************************************************************************/
#include "fleettextcommandmaker.h"

#include <QDebug>

FleetTextCommandMaker::FleetTextCommandMaker()
{

}

int FleetTextCommandMaker::makeAgvMotionLaserPath(LaserPathParameter paraIn, QString &commandStr)
{
    //组帧
    QString tmp_frame;
    QString tmp_directin;
    if(paraIn.isPositiveDirection)
    {
        tmp_directin='S';
    }
    else
    {
        tmp_directin='N';
    }
    QString tmp_translation;
    if(E_PATH_CONTROL_STRATEGY_DIFFERENTIAL==paraIn.pathControlStrategy)
    {
        tmp_translation='M';
    }
    else
    {
        tmp_translation='T';
    }
    QString tmp_attribute;
    if(0==paraIn.CNT_Value)
    {
        tmp_attribute='Q';
    }
    else
    {
        tmp_attribute='A';
    }


    QString xPos,yPos,rPos;
    xPos=QString::number(paraIn.targetPosition.x,'g',4);
    yPos=QString::number(paraIn.targetPosition.y,'g',4);
    rPos=QString::number(180.0*paraIn.targetPosition.rotate/M_PI,'g',4);

    QString isDetect;

        if(E_BARRIER_RUN_MODE_DONOTHING==paraIn.barrierRunMode)
        {
            isDetect="DONOTHING";
        }
        else
        {
            if(paraIn.isDetectBarrier)
            {
                isDetect="DE";
                isDetect+=QString::number(paraIn.barrierRunMode);
            }
            else
            {
                isDetect="NODE";
            }
        }


    QString stopAccuracy=QString::number(paraIn.allowAccuracy);
    QString vel=QString::number(paraIn.velocityRatio);
    QString pointType;

    switch(paraIn.pointSensorType)
    {
    case EM_MOVE_FREE_DP:
    {
        pointType="AM";
        break;
    }
    case EM_MOVE_FREE_VMARK:
    {
        pointType="VMARK";
        break;
    }
    case EM_MOVE_FREE_CU1:
    {
        pointType="REFLECT";
        break;
    }
    case EM_MOVE_FREE_QR:
    {
        pointType="QR";
        break;
    }
       default:
    {

    }
    }

    QString pointTypeIndex=QString::number(paraIn.pointIndex);
    QString laserFilterIndex=QString::number(paraIn.laserTypeNum);

    QString pathType;
    if(E_MOVE_FREE_MOVE_TRAJECTORY_LINE==paraIn.moveFreeMoveTrajectoryType)
    {
        pathType="LINE";
    }
    else if(E_MOVE_FREE_MOVE_TRAJECTORY_ROTATE==paraIn.moveFreeMoveTrajectoryType)
    {
        pathType="ROTATE";
    }
    else if(E_MOVE_FREE_MOVE_TRAJECTORY_SPLINE==paraIn.moveFreeMoveTrajectoryType)
    {
        pathType="SPLINE";
    }
    else
    {
        qDebug()<<"error,,,,unknow type";
        return -1;
    }

    tmp_frame='\n';
    tmp_frame+="POINT";
    tmp_frame+="#"+tmp_directin;
    tmp_frame+="#"+tmp_translation;
    tmp_frame+="#"+tmp_attribute;
    tmp_frame+=":"+xPos;
    tmp_frame+=","+yPos;
    tmp_frame+=","+rPos;
    tmp_frame+=","+pointType;
    tmp_frame+=","+pointTypeIndex;
    tmp_frame+=","+laserFilterIndex;
    tmp_frame+=","+vel;
    tmp_frame+=","+isDetect;
    tmp_frame+=","+stopAccuracy;
    tmp_frame+=","+pathType;
    tmp_frame+=","+QString::number(paraIn.stationId);
    tmp_frame+=","+QString::number(paraIn.pathId);
//    tmp_frame+=","+QString::number(paraIn.isRotatePrepare);
//    tmp_frame+=","+QString::number(paraIn.prepareAngle,'g',5);
//    tmp_frame+=","+QString::number(paraIn.controlPointRatio1,'g',2);
//    tmp_frame+=","+QString::number(paraIn.controlPointRatio2,'g',2);
    tmp_frame+=","+QString::number(paraIn.p1x,'f',3);
    tmp_frame+=","+QString::number(paraIn.p1y,'f',3);
    tmp_frame+=","+QString::number(paraIn.p2x,'f',3);
    tmp_frame+=","+QString::number(paraIn.p2y,'f',3);
    tmp_frame+='\r';
    commandStr=tmp_frame;
    return 1;
}

int FleetTextCommandMaker::makeMusic(MusicParameter paraIn, QString &commandStr)
{
    //组帧
    QString tmp_frame;
    QString musicName;
    musicName=paraIn.musicFileName;
    QString playTimes=QString::number(paraIn.musicPlayTimes);
    QString volume=QString::number(paraIn.musicVolume);

    tmp_frame='\n';
    tmp_frame+="MUSIC";
    tmp_frame+="#"+musicName;
    tmp_frame+="#"+volume;
    tmp_frame+="#"+playTimes;
    tmp_frame+='\r';
    commandStr=tmp_frame;
    return 1;
}

int FleetTextCommandMaker::makeRoller(RollerParameter paraIn, QString &commandStr)
{
    //组帧
    QString tmp_frame;
    QString moveType;
    if(E_ROLLER_LEFT_LOAD==paraIn.rollerDirectionType)
    {
        moveType="LIN";
    }
    else if(E_ROLLER_LEFT_DOWNLOAD==paraIn.rollerDirectionType)
    {
        moveType="LOUT";
    }
    else if(E_ROLLER_RIGHT_LOAD==paraIn.rollerDirectionType)
    {
        moveType="RIN";
    }
    else if(E_ROLLER_RIGHT_DOWNLOAD==paraIn.rollerDirectionType)
    {
        moveType="ROUT";
    }
    QString timeOut=QString::number(paraIn.timeOutValue);

    tmp_frame='\n';
    tmp_frame+="ROLLER";
    tmp_frame+="#"+moveType;
    tmp_frame+="#"+timeOut;
    tmp_frame+='\r';
    commandStr=tmp_frame;
    return 1;
}

int FleetTextCommandMaker::makeLift(LiftParameter paraIn, QString &commandStr)
{
    //组帧
    QString tmp_frame;
    QString mechanicalType;
//    if(E_MECHANISM_GENERAL==paraIn.mechanismType)
//    {
//        mechanicalType='A';
//    }
//    else if(E_MECHANISM_HEAVY==paraIn.mechanismType)
//    {
//        mechanicalType='B';
//    }
    QString moveType;
    if(E_LIFT_RISE==paraIn.liftType)
    {
        moveType='U';
    }
    else if(E_LIFT_DOWN==paraIn.liftType)
    {
        moveType='D';
    }
    else if(E_LIFT_RISE_MIDDLE==paraIn.liftType)
    {
        moveType="UM";
    }
    else if(E_LIFT_DOWN_MIDDLE==paraIn.liftType)
    {
        moveType="DM";
    }
    QString timeOut=QString::number(paraIn.timeOutValue);

    tmp_frame='\n';
    tmp_frame+="LIFT";
    tmp_frame+="#"+moveType;
    tmp_frame+="#"+timeOut;
    tmp_frame+='\r';
    commandStr=tmp_frame;
    return 1;
}

int FleetTextCommandMaker::makeLoadMap(LoadMapParameter paraIn, QString &commandStr)
{
    //组帧
    QString tmp_frame;
    QString mapName;
    mapName=paraIn.mapName;
    QString x=QString::number(paraIn.x);
    QString y=QString::number(paraIn.y);
    QString r=QString::number(paraIn.r);

    tmp_frame='\n';
    tmp_frame+="MAP";
    tmp_frame+="#"+mapName;
    tmp_frame+="#"+x;
    tmp_frame+="#"+y;
    tmp_frame+="#"+r;
    tmp_frame+='\r';
    commandStr=tmp_frame;
    return 1;
}

int FleetTextCommandMaker::makeCharge(ChargeParameter paraIn, QString &commandStr)
{
    //组帧
    QString tmp_frame;
    QString chargeMethod;
    if(E_CHARGING_TIME==paraIn.chargingFinishedType)
    {
        chargeMethod='T';
    }
    else if(E_CHARGING_CAPARITY==paraIn.chargingFinishedType)
    {
        chargeMethod='V';
    }
    else
    {
        chargeMethod='N';
    }
    QString chargeVolume=QString::number(paraIn.chargingCaparity);
    QString chargeTime=QString::number(paraIn.chargingTime);

    tmp_frame='\n';
    tmp_frame+="CHARGE";
    tmp_frame+="#"+chargeMethod;
    tmp_frame+="#"+chargeTime;
    tmp_frame+="#"+chargeVolume;
    tmp_frame+='\r';
    commandStr=tmp_frame;
    return 1;
}
